package com.lxh.student.controller;

import com.lxh.student.entity.Student;
import com.lxh.student.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public List<Student> getAllStudents() {
        log.info("查询所有学生");
        return studentService.findAll();
    }

    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable("id") Long id){
        return studentService.getById(id);
    }
    @PostMapping
    public HttpStatus addStudent(@RequestBody Student student) {
        log.info("put"+student.toString());
        studentService.insert(student);
        return HttpStatus.OK;
    }

    @PutMapping("/{id}")
    public HttpStatus updateStudent(@PathVariable("id") Long id, @RequestBody Student student) {
        Student existingStudent = studentService.findById(id);
        if (existingStudent == null) {
            return HttpStatus.NOT_FOUND;
        } else {
            student.setId(id);
            studentService.update(student);
            return HttpStatus.OK;
        }
    }

    @DeleteMapping("/{id}")
    public HttpStatus deleteStudent(@PathVariable("id") Long id) {
        Student existingStudent = studentService.findById(id);
        if (existingStudent == null) {
            return HttpStatus.NOT_FOUND;
        } else {
            studentService.delete(id);
            return HttpStatus.OK;
        }
    }

}


